<?php

namespace App\Http\Controllers\Guard\Shop;

use App\Http\Controllers\Common\GuardController;
use App\Models\Shop\Goods\GoodsBrand;
use App\Models\Shop\Goods\GoodsClassify;
use Illuminate\Http\Request;

class GoodsBrandController extends GuardController {

    public function lst(Request $request) {
        $conditions = $request->input();
        $records = (new GoodsBrand())->getLst($conditions)->with('getClassify:id,name')->get();
        $records->push(['id'=>0,'name'=>'全部']);
        $conditions['pid'] = 0;
        $classifies = (new GoodsClassify())->getLst($conditions)->orderBy('id')->with('children')->get();
        $classifies->push(collect(['id'=>0,'name'=>'全部','children'=>[]]));
        $this->handleChildren($classifies);
        return responseSuccess($records->toArray(),0,100000,compact('classifies'));

    }

    public function add(Request $request) {
        $id = $request->input('id',0);
        $name = $request->input('name');
        $classify_id = $request->input('classify_id');

        $this->validate($request, [
            'name'=>'required',
        ],[],[
            'name'=>'品牌名称'
        ]);
        $brand = GoodsBrand::updateOrCreate(compact('id'),compact('name','classify_id'));
        return responseSuccess($brand->toArray());
    }

    public function detail(Request $request) {
        $ids = $request->input('id',0);
        $records = (new GoodsBrand())->getLst(compact('ids'))->with(['getClassify:id,name'])->get();
        return responseSuccess($records->toArray());
    }

    public function changeStatus(Request $request) {
        $id = $request->input('id',0);
        $status = $request->input('status',2);
        GoodsBrand::where(compact('id'))->update(compact('status'));
        return responseSuccess();
    }


}
